package video;

import net.cl.tools.json.JsonArray;
import net.cl.tools.json.JsonObject;

/**
 * @author CaoLei
 * 
 */
public class Youku {

	private static final String DEST = "Youku";

	private static final String INFO_URL_PREFIX = "http://v.youku.com/player/getPlayList/VideoIDS/";

	private static final String S1 = "http://f.youku.com/player/getFlvPath/sid/%s_%s/st/%s/fileid/%s%s%s?K=%s";
	private static final String FLV = "flv";

	public Youku() {
	}

	private String genSid() {
		int i1 = (int) (1000 + Math.floor(Math.random() * 999));
		int i2 = (int) (1000 + Math.floor(Math.random() * 9000));
		return System.currentTimeMillis() + "" + i1 + "" + i2;
	}

	private String genKey(String key1, String key2) {
		int key = Long.valueOf(key1, 16).intValue();
		key ^= 0xA55AA5A5;
		return key2 + Long.toHexString(key);
	}

	private String mkFlvPath(long seg, String type, String sid, String fileid, String key) {
		String n = Long.toHexString(seg);
		while (n.length() < 2)
			n = '0' + n;
		return String.format(S1, sid, n, type, fileid.substring(0, 8), n, fileid.substring(10), key);
	}

	private String getFileIDMixString(double seed) {
		StringBuilder mixed = new StringBuilder();
		StringBuilder source = new StringBuilder(
				"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890");
		int index, len = source.length();
		for (int i = 0; i < len; ++i) {
			seed = (seed * 211 + 30031) % 65536;
			index = (int) Math.floor(seed / 65536 * source.length());
			mixed.append(source.charAt(index));
			source.deleteCharAt(index);
		}
		return mixed.toString();
	}

	private String getFileID(String fileid, double seed) {
		String mixed = getFileIDMixString(seed);
		String[] ids = fileid.split("\\*");
		StringBuilder realId = new StringBuilder();
		int idx;
		for (int i = 0; i < ids.length; i++) {
			idx = Integer.parseInt(ids[i]);
			realId.append(mixed.charAt(idx));
		}
		return realId.toString();
	}

	public String getVideoSource(String videoKey) {
		String url = INFO_URL_PREFIX + videoKey;
		JsonObject jsObj = JsonObject.connect(url);
		jsObj = jsObj.getJsonArray("data").getJsonObject(0);
		if (jsObj.containsKey("error"))
			throw new RuntimeException(jsObj.getString("error"));
		String sid = genSid();
		double seed = jsObj.getDouble("seed");
		String key1 = jsObj.getString("key1");
		String key2 = jsObj.getString("key2");
		String fileids = jsObj.getJsonObject("streamfileids").getString(FLV);
		String streamtype = FLV;
		JsonArray segs = jsObj.getJsonObject("segs").getJsonArray(FLV);
		String fileid = getFileID(fileids, seed);
		String key = genKey(key1, key2);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < segs.size(); i++) {
			if (i > 0)
				sb.append("[=]");
			String jump = mkFlvPath(segs.getJsonObject(i).getLong("no"), streamtype, sid, fileid, key);
			sb.append(jump);
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		Youku youku = new Youku();
		System.out.println(youku.getVideoSource("XMjUyODAzNDg0"));
	}

}
